#!/bin/bash
# ViperBot v1.0
# Using eggdrop 1.8 and Based on HM2k's LameBot Install Script
# ViperBot created by Poppabear @ Efnet (c) 2012

#-- Settings - Do NOT change these values unless you know what you are doing!
viper_ver="v1.0"
viper_dir=$HOME/viper
viper_tmp=viper_tmp
viper_url=http://viperbot.googlecode.com/svn/trunk
viper_eggurl=ftp://ftp.eggheads.org/pub/eggdrop/source/snapshot/
viper_egg=eggdrop1.8-snapshot.tar.gz

#-- BANNER
echo -e "\E[36m
____   ____ .__                             __________             __
\   \ /   / |__| ______     ____   _______  \______   \   ____   _/  |
 \   Y   /  |  | \____ \  _/ __ \  \_  __ \  |    |  _/  /  _ \  \   __\		
  \     /   |  | |  |_> > \  ___/   |  | \/  |    |   \ (  <_> )  |  |
   \___/    |__| |   __/   \___  >  |__|     |______  /  \____/   |__|
                 |__|          \/                   \/

Welcome to ViperBot.
ViperBot $viper_ver
Usage: viperbot <-I,-a,-u,-r> [path]
If no path is specified $HOME/viper is used

-I  Installs ViperBot (First time use only)
-a  Adds a configuration for a NEW bot
-u  Updates this file (viperbot) and viper.tcl
-r  Startup a bot

Specifying no parameters will display this banner.
Contacts:
\E[32mPoppabear @ Efnet\033[0m
"

#-- Main Script Functions
viper_help() {
	echo -e "\E[36m
		Usage: viperbot <-I,-a,-u,-r> [path]
		If no path is specified $HOME/viper is used

		-I  Installs ViperBot (First time use only)
		-a  Adds a configuration for a NEW bot
		-u  Updates this file (viperbot) and viper.tcl
		-r  Startup a bot

		Specifying no parameters will display this banner.
		Contacts:
		\E[32mPoppabear @ Efnet\033[0m
	"
}

viper_addtcl() {
	echo -e "\E[32mViperBot can add tcl scripts to your bot. Place the file you have downloaded in $viper_dir/scripts directory.\033[0m"
	echo -e "\E[32mYou will need to provide the FILENAME of the file downloaded (ie: google_tcl.tar.gz)\033[0m"
	echo -e "\E[32mYou will also need to provide the BOTNICK of the bot you wish to load the script on.\033[0m"
	echo " "
	echo -en "\E[32mWould you like to add a script now? (y/n): \033[0m"
	read yesno;

	if [[ $yesno = "y" || $yesno = "Y" ]]; then
		read -p "ViperBot Nickname: " viper_nick
		read -p "FileName (ie: google_tcl.tar.gz): " viper_script
		if [ ! -e $viper_dir/scripts/$viper_script ]; then
			echo -e "\E[31mERROR: Can't locate $viper_dir/scripts/$viper_script !\033[0m"
			exit $?
		fi

		case "${viper_script#*.}" in
			"gz" )
			    if [ ! `which gunzip` ]; then
                                echo -e "\E[31mERROR: Your system doesn't have gunzip installed. This is required to uncompress a .gz file!\033[0m"
                                exit $?
                            fi
			    gunzip $viper_dir/scripts/$viper_script
			;;
			"tar" | "tar.gz" )
			    tar zxvf $viper_dir/scripts/$viper_script
			;;
			"zip" )
			    if [ ! `which unzip` ]; then
				echo -e "\E[31mERROR: Your system doesn't have unzip installed. This is required to uncompress a .zip file!\033[0m"
				exit $?
			    fi
			    unzip $viper_dir/scripts/$viper_script
			;;
			* )
			    echo -e "\E[31mERROR: File type ${viper_script#*.} is not supported in this version!\033[0m"
			    exit $?
			;;
		esac

		echo source "${viper_script%%.*}".tcl >> $viper_dir/$viper_nick.conf
		# restart the bot here
	else
		exit $?
	fi 
}

viper_eggget(){
	echo -e "\E[32mViperBot will now attempt to download $viper_egg \033[0m"
	if [ -e $viper_tmp/$viper_egg ]; then
		echo -e "\E[34mExtracting Eggdrop...\033[0m"
                tar zxvf $viper_tmp/$viper_egg -C $viper_tmp
                rm -f $viper_tmp/$viper_egg
        else
                viper_mkdir $viper_tmp
                viper_get $viper_eggurl/$viper_egg $viper_tmp/$viper_egg
                viper_eggget
        fi

}

viper_mkdir() {
	if [ ! -d $1 ]; then
 		mkdir $1
	fi
	if [ ! -d $1 ]; then
		echo -e "\E[31mERROR: Unable to make directory $1!\033[0m"
		exit $?
	fi
}

viper_get() {
	if hash wget 2>&- ; then
		wget -t0 -O $2 $1
	elif hash fetch 2>&- ; then
		fetch -o $2 $1
	elif hash curl 2>&- ; then
		curl -o $2 $1
	elif hash lynx 2>&- ; then
		lynx -source $1 > $2
	else
		echo -e "\E[31mFATAL ERROR: Unable to find wget, fetch, curl or lynx.\033[0m"
		exit $?
	fi
}

viper_egginstall(){
	viper_mkdir $viper_tmp
	viper_cwd=$PWD
	echo -e "\E[32mInstalling Eggdop...\033[0m"
	cd $viper_tmp/eggdrop*.*
	echo -e "......................................."
  echo -e "Here you need to choose if you will be using TLS (SSL). If you choose to disable now, you can NOT re-enable later."
  echo -en "Would you like to DISABLE TLS (y/n)?"
  read yn

 if [[ $yn = "n" || $yn = "N" || $yn = "no" || $yn = "No" ]]; then
   ./configure
    viper_makecmd sslcert
 else
   ./configure --disable-tls
 fi

	viper_makecmd config
	viper_makecmd static
	viper_makecmd install DEST=$viper_dir
	cd $viper_cwd
}

# Fixed to support FreeBSD's gmake
viper_makecmd(){
OS=`uname`

	if [ $OS = "FreeBSD" ]; then
		gmake $1 $2
	else
		make $1 $2
	fi
}

viper_eggsetup(){
	if [ -d $viper_tmp ]; then
  		#cleanup
		rm -fr $viper_tmp
	fi
	if [ -d $viper_dir ]; then
		#setup
		cp ~/viperbot $viper_dir/viperbot
		viper_get $viper_url/motd $viper_dir/text/motd
		viper_get $viper_url/banner $viper_dir/text/banner
		rm -f README
		rm -f $viper_dir/eggdrop.conf
		rm -fr $viper_dir/doc
		mv -f $viper_dir/eggdrop $viper_dir/viper
	fi
}

viper_getbotnet(){
	if [ -d $viper_dir ]; then
		if [ ! -e $viper_dir/botnet ]; then
			echo -e "\E[33mNOTE: Be sure to edit $viper_dir/botnet BEFORE adding your first bot.\033[0m"
			echo -e "\E[33mYour first 2 bots that you add MUST match whats in the botnet file.\033[0m"
      echo -e "\E[33mYour botnet bots should use IPv4 Addresses, This should allow them to link without issues.\033[0m"
      echo -e "\E[33mIf you need help with this please come see us at #viperbot on Efnet.\033[0m"
			echo " "
			read -p "Where is your botnet file? (eg: $viper_url/botnet) " viperbotnet
			viper_get $viperbotnet $viper_dir/botnet
		fi
		if [ ! -e $viper_dir/botnet ]; then
			viper_get $viper_url/botnet $viper_dir/botnet
		fi
	fi
}

viper_getcore(){
	if [ -d $viper_dir ]; then
		if [ -e $viper_dir/viper.tcl ]; then
			mv $viper_dir/viper.tcl $viper_dir/viper.tcl.old
		fi

		viper_get $viper_url/viper.tcl $viper_dir/viper.tcl
		viper_get $viper_url/viperbot.conf $viper_dir/viperbot.conf

		if [ ! -e $viper_dir/viper.tcl ]; then
			mv $viper_dir/viper.tcl.old $viper_dir/viper.tcl
		fi
	fi
}

viper_conf(){
	read -p "ViperBot Nickname: " viper_nick
	viper_conf=$viper_dir/$viper_nick.conf
	if [ -e $viper_conf ]; then
		mv $viper_conf $viper_conf.old
	fi
	echo set nick '"'$viper_nick'"'>> $viper_conf
	echo set altnick '"'$viper_nick-'"'>> $viper_conf
	echo set userfile '"'$viper_nick.user'"'>> $viper_conf
	echo set chanfile '"'$viper_nick.chan'"'>> $viper_conf
	echo set notefile '"'$viper_nick.notes'"'>> $viper_conf
	echo set username '"'$viper_nick'"' >> $viper_conf

	read -p "ViperBot Realname: " viper_realname
	echo set realname '"'$viper_realname'"' >> $viper_conf

	read -p "Set Owner (your irc nick): " viper_owner
        echo set owner '"'$viper_owner'"' >> $viper_conf
	echo set admin '"'$viper_owner'"' >> $viper_conf
	echo set notify-newusers '"'$viper_owner'"' >> $viper_conf

	echo -en "Would you like to use IPv6 for this bot? (y/n): "
	read yesno;

	if [[ $yesno = "y" || $yesno = "Y" ]]; then
        	read -p "Set vhost6 (This can be the hostname or IPv6 Address): " viper_ipv6
        	echo set vhost6 '"'$viper_ipv6'"' >> $viper_conf
		      echo set prefer-ipv6 1 >> $viper_conf
	else
          read -p "Set vhost4 (This can be the hostname or IPv4 Address): " viper_ipv4
          echo set vhost4 '"'$viper_ipv4'"' >> $viper_conf
		      echo set prefer-ipv6 0 >> $viper_conf
	fi
   
  read -p "ViperBot listening Address (Must be IPv4 in order DCC Chat/Telnet): " viper_listen 
        echo set listen-addr '"'$viper_listen'"' >> $viper_conf
	read -p "ViperBot listening PORT: " viper_port
        echo listen '"'$viper_port'"' all >> $viper_conf

	echo source viperbot.conf >> $viper_conf
	echo source botnet >> $viper_conf
	chmod 666 $viper_conf

	if [ `pwd` == "$viper_dir" ]; then
		viper_run
	else
		cd $viper_dir
		$viper_dir/viper -m $viper_conf
		echo -e "\E[32mFinished, Created '$viper_nick.conf' - Should be running!\033[0m"
	fi
}

viper_run(){
	if [ ! -e $viper_dir/viper ]; then
		echo -e "\E[33mWARNING: Could not find 'viper' in $viper_dir/, check for errors...\033[0m"
		exit $?
	fi

  viper_conf=$viper_nick.conf

  if [ -e $viper_dir/$viper_conf ]; then
    echo -e "\E[32mStarting bot ...\033[0m"
    $viper_dir/viper -m $viper_conf
    echo -e "\E[32mFinished, Created '$viper_nick.conf' - Should be running!\033[0m"
  else
    echo -e "\E[33mWARNING: Could not find '$viper_conf', check for errors...\033[0m"
  fi
}

viper_install(){

	if [ -d $viper_dir ]; then
		echo -e "\E[31mERROR: ViperBot is already installed! If you need to re-install delete $viper_dir and run \"bash ~/viper -I\"!\033[0m"
		exit $?
	else
		if [ ! -e $viper_tmp/eggdrop ]; then
			if [ ! -e $viper_tmp/$viper_egg ]; then
				viper_eggget
			fi
			viper_egginstall
		fi
		viper_eggsetup
		viper_getcore
		viper_getbotnet
	fi
}

viper_new(){
	if [ ! -d $viper_dir ]; then
		echo -e "\E[31mERROR: Directory does not exist! Make sure ViperBot was installed correctly, Make sure $viper_dir exists!\033[0m"
		exit $?
	else
		viper_getcore
		viper_getbotnet
		viper_conf
	fi
}

viper_update(){
	viper_botupdate
	viper_getcore
	viper_rehash
	echo -e "\E[32mviperbot and viper.tcl WAS UPDATED\033[0m"
}

viper_botupdate(){
	mv -f $0 viperbot.old
	viper_get $viper_url/viperbot $0
	chmod 755 viperbot
}

viper_rehash(){
	kill -HUP `cat $viper_dir/pid.*`
}

toLower() { echo $1 | tr '[A-Z]' '[a-z]'; }

if [ $1 ]; then
	if [ $2 ]; then
		viperdir=$2
	fi
	if [ $1 = "-I" ]; then
		viper_install
	elif [ $1 = "-a" ]; then
		viper_new
	elif [ $1 = "-u" ]; then
		viper_update
	elif [ $1 = "-s" ]; then
		viper_addtcl
	elif [ $1 = "-r" ]; then
		if [ $2 ]; then
			viper_nick=$2
		else
			read -p "ViperBot Nickname: " viper_nick
		fi
		viper_run $viper_dir/$viper_nick
	else 
		viper_help
	fi
fi

exit 0
#EOF

